﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AhoraSiMvc3.Models;
using System.Data.Entity;

namespace AhoraSiMvc3.DataAccess
{
    public class StudentsRepo : IStudentsRepo
    {
        private IUnitOfWorkFactory uowFactory;

        public StudentsRepo(IUnitOfWorkFactory uowFactory)
        {
            this.uowFactory = uowFactory;
        }

        public ModelContext DbContext
        {
            get { return (this.uowFactory.Current as UnitOfWork).DbContext; }
        }

        public DbSet<Student> Set {
            get { return this.DbContext.Students; } 
        }

        public Student Get(int id)
        {
            return this.Set.Find(id);
        }

        public Student Add(Student student)
        {
            return this.Set.Add(student);
        }

        public void Update(Student student)
        {
            this.DbContext.Entry(student).State = System.Data.EntityState.Modified;
        }

        public void Remove(Student student)
        {
            this.Set.Remove(student);
        }

        public IList<Student> GetAll()
        {
            return this.Set.ToList();
        }
    }
}